﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BitAuto.EP.CRMLogSystem.BLL;
using BitAuto.EP.CRMLogSystem.Entity;
using BitAuto.EP.CRMLogSystem.Utils;
using Newtonsoft.Json.Linq;
using BitAuto.EP.Plat.Util;

namespace CRMSimple.LogServices
{
    public static class InPutDB
    {
        private static ConfigurationManager config = Singleton<ConfigurationManager>.Instance;
        public static void BulkToDB(IList<LogTableInfo> info,int count=0)
        {
            IDictionary<string, Exception> dic= InputDBBLL.BulkToDB(info);
            if (dic != null)
            {
                count++;
                var res = (from e in info
                           where dic.ContainsKey(e.LogTableId)
                           select e
                             ).ToList();
                if (count > 2)
                {
                    //写入错误配置文件
                    foreach (var item in res)
                    {
                        item.AddExceptionFile(config);
                        //记录异常
                        string content=string.Format("入库异常,异常数据文件路径：{0}",item.FilePath);
                        //DataLogHelper.E("入库异常", dic[item.LogTableId]);
                        LogExceptionRoot.DataConsumerLog.Error(content, dic[item.LogTableId]);

                        ErrorAlertMailHelper.SendAlertMail("日志服务入库异常", content + "异常：" + dic[item.LogTableId].Message + dic[item.LogTableId].StackTrace);
                        item.Clear();
                    }
                }else
                    BulkToDB(res,count);
            }
        }
        
    }
}
